﻿using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x020003AC RID: 940
	public class BehaviorManagerView : MonoBehaviour
	{
		// Token: 0x060018D9 RID: 6361 RVA: 0x0009C9EC File Offset: 0x0009ABEC
		private void Initialize()
		{
			Button[] componentsInChildren = base.GetComponentsInChildren<Button>();
			this.editButton = componentsInChildren[0];
			this.editButton.onClick.AddListener(new UnityAction(this.EditBehavior));
			Button button = componentsInChildren[1];
			button.onClick.AddListener(new UnityAction(this.Close));
			this.buttonPrefab = Resources.Load<Button>("UI/BehaviorMgr/BehMgrItem");
			this.listCanvas = base.GetComponentInChildren<GridLayoutGroup>().transform;
			this.initialized = true;
			this.selectedBehavior = null;
		}

		// Token: 0x060018DA RID: 6362 RVA: 0x0009CA70 File Offset: 0x0009AC70
		private void Start()
		{
			this.settingsView = UnityEngine.Object.Instantiate<GameObject>(Resources.Load("UI/BehaviorMgr/BehaviorSettingsMenu") as GameObject);
			this.settingsView.transform.SetParent(base.transform, false);
			this.settingsView.AddComponent<BehaviorSettingsView>();
			this.settingsView.SetActive(false);
			this.settingsView.GetComponent<BehaviorSettingsView>().Initialize();
		}

		// Token: 0x060018DB RID: 6363 RVA: 0x0009CAD8 File Offset: 0x0009ACD8
		private void sortAlphabetically()
		{
			List<GameObject> list = new List<GameObject>();
			for (int i = 0; i < this.listCanvas.transform.childCount; i++)
			{
				list.Add(this.listCanvas.transform.GetChild(i).gameObject);
			}
			list.Sort(delegate(GameObject a, GameObject b)
			{
				if (a == null && b == null)
				{
					return 0;
				}
				if (a == null)
				{
					return -1;
				}
				if (b == null)
				{
					return 1;
				}
				return a.GetComponent<TextMeshProUGUI>().text.CompareTo(b.GetComponent<TextMeshProUGUI>().text);
			});
			for (int j = 0; j < list.Count; j++)
			{
				list[j].transform.SetSiblingIndex(j);
			}
		}

		// Token: 0x060018DC RID: 6364 RVA: 0x0009CB74 File Offset: 0x0009AD74
		public void ReloadBehaviorList()
		{
			List<IBehavior> list = BehaviorLists.Instance.GetAllBehaviors().FindAll((IBehavior beh) => beh.CanAppearInBehaviorManager());
			Button[] componentsInChildren = this.listCanvas.GetComponentsInChildren<Button>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
			}
			list.ForEach(delegate(IBehavior bObj)
			{
				Button button = UnityEngine.Object.Instantiate<Button>(this.buttonPrefab);
				button.transform.SetParent(this.listCanvas.transform, false);
				TextMeshProUGUI componentInChildren = button.GetComponentInChildren<TextMeshProUGUI>();
				componentInChildren.text = bObj.GetText();
				button.onClick.AddListener(delegate()
				{
					this.OnClickElement(bObj);
				});
				if (bObj.CanUseAI())
				{
					button.GetComponentInChildren<Toggle>().isOn = bObj.IsAI();
					button.GetComponentInChildren<Toggle>().onValueChanged.AddListener(delegate(bool val)
					{
						LuaBehavior luaBehavior = bObj as LuaBehavior;
						luaBehavior.ai = val;
						IOManager.Instance.SaveSettings(luaBehavior);
					});
				}
				else
				{
					button.GetComponentInChildren<Toggle>().isOn = true;
					button.GetComponentInChildren<Toggle>().interactable = false;
					(button.GetComponentInChildren<Toggle>().graphic as Image).sprite = Resources.Load<Sprite>("UI/BehaviorMgr/BM_DisabledCheck");
				}
			});
			this.sortAlphabetically();
		}

		// Token: 0x060018DD RID: 6365 RVA: 0x0009CBF3 File Offset: 0x0009ADF3
		private void Update()
		{
			if (Input.GetKeyDown(KeyCode.Return))
			{
				this.EditBehavior();
			}
		}

		// Token: 0x060018DE RID: 6366 RVA: 0x0009CC08 File Offset: 0x0009AE08
		private void OnEnable()
		{
			if (!this.initialized)
			{
				this.Initialize();
			}
			else
			{
				if (this.settingsView != null)
				{
					this.settingsView.SetActive(false);
				}
				base.GetComponentInChildren<Text>().text = "Behavior Manager";
			}
			this.ReloadBehaviorList();
		}

		// Token: 0x060018DF RID: 6367 RVA: 0x0009CC5E File Offset: 0x0009AE5E
		private void OnClickElement(IBehavior bObject)
		{
			if (this.selectedBehavior == bObject)
			{
				this.EditBehavior();
				return;
			}
			this.selectedBehavior = bObject;
		}

		// Token: 0x060018E0 RID: 6368 RVA: 0x0009CC7A File Offset: 0x0009AE7A
		private void Close()
		{
			this.settingsView.SetActive(false);
			base.gameObject.SetActive(false);
		}

		// Token: 0x060018E1 RID: 6369 RVA: 0x0009CC94 File Offset: 0x0009AE94
		private void EditBehavior()
		{
			if (this.selectedBehavior == null)
			{
				return;
			}
			this.settingsView.SetActive(true);
			this.settingsView.GetComponent<BehaviorSettingsView>().SetBehavior(this.selectedBehavior);
			base.GetComponentInChildren<Text>().text = this.selectedBehavior.GetText();
		}

		// Token: 0x04001AE6 RID: 6886
		private Button editButton;

		// Token: 0x04001AE7 RID: 6887
		private bool initialized;

		// Token: 0x04001AE8 RID: 6888
		private Button buttonPrefab;

		// Token: 0x04001AE9 RID: 6889
		private Transform listCanvas;

		// Token: 0x04001AEA RID: 6890
		private GameObject settingsView;

		// Token: 0x04001AEB RID: 6891
		private IBehavior selectedBehavior;
	}
}
